﻿using System;
using System.Collections.Generic;
using System.IO;
using JamesZhao.Engine.Graphics.Geometry;
using SlimMath;

namespace JamesZhao.Engine.Loaders
{
    /// <summary>
    /// Loads .dae mesh/model files
    /// </summary>
    public class ObjMeshloader : MeshLoader
    {
        private static readonly Random random = new Random();

        public Vector4 RandomUNormVec4()
        {
            return new Vector4((float) random.NextDouble(), (float) random.NextDouble(), (float) random.NextDouble(), 1);
        }

        public override MeshData LoadModel(string path)
        {
            var mesh = new MeshData();
            string[] content = File.ReadAllLines(path);
            var normals = new List<Vector3>();
            var uvs = new List<Vector2>();
            foreach (string line in content)
            {
                string[] splittedLine = line.Split(new[] {' '});
                float x, y, z;
                switch (splittedLine[0])
                {
                    case "o":
                        break;
                    case "v":
                        x = Convert.ToSingle(splittedLine[1]);
                        y = Convert.ToSingle(splittedLine[2]);
                        z = Convert.ToSingle(splittedLine[3]);
                        mesh.Vertices.Add(new Vertex
                                              {
                                                  Color = new Vector4(.8f, 1f, .8f, 1),
                                                  //RandomUNormVec4(),
                                                  Position = new Vector4(x, y, z, 1)
                                              });
                        break;
                    case "vn":
                        x = Convert.ToSingle(splittedLine[1]);
                        y = Convert.ToSingle(splittedLine[2]);
                        z = Convert.ToSingle(splittedLine[3]);
                        normals.Add(new Vector3(x, y, z));
                        break;
                    case "vt":
                        x = Convert.ToSingle(splittedLine[1]);
                        y = Convert.ToSingle(splittedLine[2]);
                        uvs.Add(new Vector2(x, y));
                        break;
                    case "f":
                        var triangle = new Triangle();

                        for (int i = 1; i < 4; i++)
                        {
                            string[] faceStr = splittedLine[i].Split(new[] {'/'});
                            int v = Convert.ToInt32(faceStr[0]) - 1;
                            //int u = Convert.ToInt32(faceStr[1]) - 1; //uv
                            int n = Convert.ToInt32(faceStr[2]) - 1;
                            var vert = new Vertex
                                           {
                                               Color = mesh.Vertices[v].Color,
                                               Normal = normals[n],
                                               Position = mesh.Vertices[v].Position
                                           };
                            mesh.Vertices[v] = vert;
                            triangle.Verts[i - 1] = v;
                        }
                        mesh.Triangles.Add(triangle);
                        break;
                }
            }
            return mesh;
        }

        public override string GetExtension()
        {
            return "obj";
        }
    }
}